package com.gscitysfy.cus.drain.task;

import com.gscitysfy.cloudfrmwk.portal.feign.DistrictService;
import com.gscitysfy.cloudfrmwk.portal.vo.DistrictVO;
import com.gscitysfy.cus.drain.constants.DistrictConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
@Order(value = 1)
public class LoadCacheData implements CommandLineRunner {

    /**
     * 日志
     */
    private static final Logger logger = LoggerFactory.getLogger(LoadCacheData.class);

    @Resource
    private DistrictService districtService;

    @Override
    public void run(String... arg0) throws Exception {
        loadDistrict();
    }

    /**
     * portal_mn_dictionary
     * 加载区划数据到内存.
     */
    @Scheduled(cron = "0 0 */5 * * ?")
    public void loadDistrict() {
        logger.info("启动初始化区划数据字典数据------>>>start");
        DistrictConverter.allConstants.clear();
        List<DistrictVO> allDistrictVOList = districtService.getAllList().getData();
        if(null!=allDistrictVOList){
            allDistrictVOList.forEach(districtVO -> {
                DistrictConverter.setAllConstants(districtVO.getCode()+"",districtVO.getName());
            });
        }
        logger.info("启动初始化区划数据字典数据------>>>end");
    }

}
